import Vue from 'vue'
import Router from 'vue-router'
/* Layout */
import Layout from '@/layout'
Vue.use(Router)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' },
    hidden: true
  },
  {
    path: '/sso',
    component: () => import('@/views/login/sso'),
    meta: { title: '登录' },
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/login/register'),
    meta: { title: '注册' },
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    meta: { title: '404' },
    hidden: true
  },
  {
    path: '/',
    component: () => import('@/nav'),
    redirect: '/dashboard',
    meta: { title: '概况' },
    children: [{
      path: '/dashboard',
      component: () => import('@/views/dashboard'),
      meta: { title: '概况' }
    }]
  }
]

/*
  动态权限列表
  *bPath:对应的后台地址
  1.根据bPath 与 后台路由进行对比  相等则显示
  2.hidden:true 菜单无需设置bPath
*/
export const asyncRoutes = [
  // {
  //   name: 'dashboard',
  //   path: '/dashboard',
  //   component: () => import('@/views/dashboard'),
  //   meta: { title: '概况' }
  // },
  {
    name: 'source',
    path: '/code',
    component: () => import('@/nav'),
    meta: { title: '代码' },
    bPath: '/git/list',
    redirect: '/repository',
    children: [
      {
        name: 'repository',
        path: '/repository',
        component: () => import('@/views/code/repository'),
        meta: { title: '托管' },
        bPath: '/git/list'
      },
      {
        name: 'branchSetting',
        path: '/code/branch/:pid',
        component: () => import('@/views/code/branch'),
        meta: { title: '分支设置' },
        bPath: '/git/list',
        hidden: false
      },
      {
        name: 'git',
        path: '/git/:pid/:pname/:branch/:path',
        component: () => import('@/views/code/repository/git'),
        meta: { title: '仓库' },
        bPath: '/git/list',
        hidden: true,
        redirect: '/git/code/:pid/:pname/:branch/:path',
        children: [
          {
            name: 'code',
            path: '/git/code/:pid/:pname/:branch/:path',
            component: () => import('@/views/code/repository/pages/code'),
            meta: { title: '源文件' },
            bPath: '/git/list'
          },
          {
            name: 'commit',
            path: '/git/commit/:pid/:pname',
            component: () => import('@/views/code/repository/pages/commit'),
            meta: { title: '提交' },
            bPath: '/git/list'
          },
          {
            name: 'branch',
            path: '/git/branch/:pid/:pname',
            component: () => import('@/views/code/repository/pages/branch'),
            meta: { title: '分支' },
            bPath: '/git/list'
          },
          {
            name: 'tag',
            path: '/git/tag/:pid/:pname',
            component: () => import('@/views/code/repository/pages/tag'),
            meta: { title: '标签' },
            bPath: '/git/list'
          },
          {
            name: 'merge',
            path: '/git/merge/:pid/:pname',
            component: () => import('@/views/code/repository/pages/merge'),
            meta: { title: '合并请求' },
            bPath: '/git/list'
          },
          {
            name: 'wiki',
            path: '/git/wiki/:pid/:pname',
            component: () => import('@/views/code/repository/pages/wiki'),
            meta: { title: '文档' },
            bPath: '/git/list'
          },
          {
            name: 'member',
            path: '/git/member/:pid/:pname',
            component: () => import('@/views/code/repository/pages/member'),
            meta: { title: '成员' },
            bPath: '/git/list'
          },
          {
            name: 'webhook',
            path: '/git/webhook/:pid/:pname',
            component: () => import('@/views/code/repository/pages/webhook'),
            meta: { title: '设置' },
            bPath: '/git/list'
          },
          {
            name: 'ssh',
            path: '/git/ssh/:pid/:pname',
            component: () => import('@/views/code/repository/pages/ssh'),
            meta: { title: 'SSH密钥' },
            bPath: '/git/list'
          }
        ]
      }
    ]
  },
  {
    name: 'build',
    path: '/build',
    component: () => import('@/nav'),
    meta: { title: '构建' },
    bPath: '/pipeline/list',
    redirect: '/build/pipeline',
    children: [
      {
        name: 'pipeline',
        path: '/build/pipeline',
        component: () => import('@/views/build/pipeline'),
        meta: { title: '流水线' },
        bPath: '/pipeline/list'
      },
      {
        name: 'run',
        path: '/build/pipeline/run/:id',
        component: () => import('@/views/build/pipeline/run'),
        meta: { title: '运行' },
        hidden: true
      },
      {
        name: 'history',
        path: '/build/pipeline/history/:id',
        component: () => import('@/views/build/pipeline/history'),
        meta: { title: '运行日志' },
        hidden: true
      },
      {
        name: 'env',
        path: '/build/env',
        component: () => import('@/views/build/env'),
        meta: { title: '环境' },
        hidden: true
      },
      {
        name: 'config',
        path: '/build/config',
        component: () => import('@/views/build/env/config'),
        meta: { title: '配置' },
        hidden: true
      }
    ]
  },
  {
    name: 'test',
    path: '/test',
    component: () => import('@/nav'),
    meta: { title: '测试' },
    redirect: '/test/list',
    hidden: true,
    children: [
      {
        path: '/test/list',
        component: () => import('@/views/test'),
        meta: { title: '测试' }
      }
    ]
  },
  {
    name: 'setting',
    path: '/setting',
    component: () => import('@/nav'),
    meta: { title: '设置' },
    bPath: '/user/list',
    redirect: '/user/list',
    children: [
      {
        path: '/user/list',
        component: () => import('@/views/user/list'),
        meta: { title: '账号列表' },
        bPath: '/user/list'
      },
      {
        path: '/user/role',
        component: () => import('@/views/user/role'),
        meta: { title: '角色列表' },
        bPath: '/user/role'
      },
      {
        path: '/pipeline/template',
        component: () => import('@/views/build/template/template'),
        meta: { title: '阶段模板' },
        bPath: '/pipeline/template'
      },
      {
        path: '/pipeline/flow',
        component: () => import('@/views/build/template/flow'),
        meta: { title: '默认流水' },
        bPath: '/pipeline/flow/list'
      },
      {
        path: '/setting/email',
        component: () => import('@/views/setting/email'),
        meta: { title: '邮箱配置' },
        bPath: ''
      }
    ]
  },
  // {
  //   path: '/user',
  //   redirect: '/user/list',
  //   meta: { title: '账号管理', icon: 'user' },
  //   bPath: '/user/list',
  //   children: [
  //     {
  //       path: 'list',
  //       component: () => import('@/views/user/list'),
  //       meta: { title: '账号列表' },
  //       bPath: '/user/list'
  //     },
  //     {
  //       path: 'role',
  //       component: () => import('@/views/user/role'),
  //       meta: { title: '角色列表' },
  //       bPath: '/user/role'
  //     }
  //   ]
  // },
  {
    name: 'sonar',
    path: '/sonar',
    component: () => import('@/views/build/pipeline/modals/qFrame'),
    meta: { title: 'sonar' },
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
